import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/AboutView.vue'),
      meta: {
        isLogin: true
      }
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue')
    },
    {
      path: '/my',
      name: 'my',
      component: () => import('../views/MyView.vue'),
      meta: {
        isLogin: true
      }
    },
    {
      path: '/video',
      name: 'video',
      component: () => import('../views/VideoView.vue')
    },
    {
      path: '/zc',
      name: 'zc',
      component: () => import('../views/ZcView.vue')
    },
    {
      path: '/xq/:arr',
      name: 'xq',
      component: () => import('../views/XqView.vue'),
      props: true
    },
    {
      path: '/ceshi',
      name: 'ceshi',
      component: () => import('../views/CeshiView.vue')
    },
    {
      path: '/bpp',
      name: 'bpp',
      component: () => import('../views/BppView.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.isLogin) {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router
